﻿using PayexpDAL;
using RoyaleLiving.Models.DomainModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace RoyaleLiving.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        //public ActionResult IncomeExpenseSummary()
        //{
        //    ViewBag.Message = "Income Expense Summary";

        //    DataAccessHandler dataAccessHandler = new DataAccessHandler();
        //    using(AlfaRomeoEntities context = new AlfaRomeoEntities())
        //    {
        //        List<TransactionMaster> trasanctionMasterList = dataAccessHandler.GetTransactionMasterListAll(context);
        //        foreach (var transactionMaster in trasanctionMasterList)
        //        {
        //            transactionMaster.CurrentBalance = dataAccessHandler.GetCurrentBalance(transactionMaster, context);
        //        }

        //        return View(trasanctionMasterList);
        //    }
        //}

        //[ActionName("IncomeExpenseSummaryAction")]
        [HttpGet]
        public ActionResult IncomeExpenseSummary(int page = 1)
        //public ActionResult IncomeExpenseSummary()
        {
            ViewBag.Message = "Income Expense Summary";
            int numberOfRows = 10;
            int totalRows = 0;

            IncomeExpenseDetails incomeExpenseDetails = new IncomeExpenseDetails();
            incomeExpenseDetails.PageSize = numberOfRows;
            incomeExpenseDetails.CurrentPage = page-1;
            DataAccessHandler dataAccessHandler = new DataAccessHandler();
            //using (AlfaRomeoEntities context = new AlfaRomeoEntities())
            {
                AlfaRomeoEntities context = new AlfaRomeoEntities();
                List<TransactionMaster> transactionMasterList = new List<TransactionMaster>();
                transactionMasterList.AddRange(dataAccessHandler.GetTransactionMasterListAll(page - 1,
                                                                                                              numberOfRows,
                                                                                                              out totalRows,
                                                                                                              context));
                foreach (var transactionMaster in transactionMasterList)
                {
                    transactionMaster.CurrentBalance = dataAccessHandler.GetCurrentBalance(transactionMaster, context);

                    IncomeExpenseRow incomeExpenseRow = new IncomeExpenseRow();
                    incomeExpenseRow.ChequeDetails = transactionMaster.ConsolidateChequeDetails;
                    incomeExpenseRow.CurrentBalance = transactionMaster.CurrentBalance;
                    incomeExpenseRow.IncomeExpense = transactionMaster.HeadDisplayName;
                    incomeExpenseRow.Narration = transactionMaster.Narration;
                    incomeExpenseRow.TransactionAmount = transactionMaster.TransactionAmount;
                    incomeExpenseRow.TransactionDate = transactionMaster.TransactionDateDDMonthYY;
                    incomeExpenseRow.TransactionHeadDetails = transactionMaster.HeadDetailsDisplayName;
                    incomeExpenseRow.TransactionMode = transactionMaster.TransactionModeValueDisplayName;
                    incomeExpenseDetails.IncomeExpenseRows.Add(incomeExpenseRow);

                }

                incomeExpenseDetails.TotalRows = totalRows;

                if (page == 1)
                {
                    return View(incomeExpenseDetails);
                }
                else
                {
                    var result = Json(incomeExpenseDetails.IncomeExpenseRows, "application/json", JsonRequestBehavior.AllowGet);
                    return result;
                }
            }
        }


    }
}
